home *** CD-ROM | disk | FTP | other *** search
/ Workbench Add-On / Workbench Add-On - Volume 1.iso / BBS-Archive / Comm / AmiTCP30b2.lha / src / rpclib / clnt_simple.c < prev    next >
C/C++ Source or Header  |  1994-03-09  |  4KB  |  134 lines

  1. /*
  2.  * $Id: clnt_simple.c,v 1.3 1994/02/25 00:21:41 jraja Exp $
  3.  *
  4.  * $Log: clnt_simple.c,v $
  5.  * Revision 1.3  1994/02/25  00:21:41  jraja
  6.  * Changed malloc, calloc and free to mem_alloc, mem_calloc and mem_free,
  7.  * respectively.
  8.  *
  9.  * Revision 1.2  1993/11/10  01:33:46  jraja
  10.  * Fixed includes and some types.
  11.  * Changes close() to CloseSocket() for the AMITCP.
  12.  *
  13.  */
  14. /* @(#)clnt_simple.c    2.2 88/08/01 4.0 RPCSRC */
  15. /*
  16.  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
  17.  * unrestricted use provided that this legend is included on all tape
  18.  * media and as a part of the software program in whole or part.  Users
  19.  * may copy or modify Sun RPC without charge, but are not authorized
  20.  * to license or distribute it to anyone else except as part of a product or
  21.  * program developed by the user.
  22.  * 
  23.  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
  24.  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
  25.  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
  26.  * 
  27.  * Sun RPC is provided with no support and without any obligation on the
  28.  * part of Sun Microsystems, Inc. to assist in its use, correction,
  29.  * modification or enhancement.
  30.  * 
  31.  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
  32.  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
  33.  * OR ANY PART THEREOF.
  34.  * 
  35.  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
  36.  * or profits or other special, indirect and consequential damages, even if
  37.  * Sun has been advised of the possibility of such damages.
  38.  * 
  39.  * Sun Microsystems, Inc.
  40.  * 2550 Garcia Avenue
  41.  * Mountain View, California  94043
  42.  */
  43. #if !defined(lint) && defined(SCCSIDS)
  44. static char sccsid[] = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
  45. #endif
  46.  
  47. /* 
  48.  * clnt_simple.c
  49.  * Simplified front end to rpc.
  50.  *
  51.  * Copyright (C) 1984, Sun Microsystems, Inc.
  52.  */
  53.  
  54. #include <sys/param.h>
  55. #include <stdio.h>
  56. #include <rpc/rpc.h>
  57. #include <sys/socket.h>
  58. #include <netdb.h>
  59.  
  60. /* Single element cache
  61.  */
  62. static struct callrpc_private {
  63.     CLIENT    *client;
  64.     int    socket;
  65.     int    oldprognum, oldversnum, valid;
  66.     char    *oldhost;
  67. } *callrpc_private;
  68.  
  69. int
  70. callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
  71.     const char * host;
  72.     u_long prognum, versnum, procnum;
  73.     xdrproc_t inproc, outproc;
  74.     void * in, * out;
  75. {
  76.     register struct callrpc_private *crp = callrpc_private;
  77.     struct sockaddr_in server_addr;
  78.     enum clnt_stat clnt_stat;
  79.     struct hostent *hp;
  80.     struct timeval timeout, tottimeout;
  81.  
  82.     if (crp == 0) {
  83.         crp = (struct callrpc_private *)mem_calloc(1, sizeof (*crp));
  84.         if (crp == 0)
  85.             return (0);
  86.         callrpc_private = crp;
  87.     }
  88.     if (crp->oldhost == NULL) {
  89.         crp->oldhost = mem_alloc(256);
  90.         crp->oldhost[0] = 0;
  91.         crp->socket = RPC_ANYSOCK;
  92.     }
  93.     if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
  94.         && strcmp(crp->oldhost, host) == 0) {
  95.         /* reuse old client */        
  96.     } else {
  97.         crp->valid = 0;
  98. #ifdef AMITCP
  99.         (void)CloseSocket(crp->socket);
  100. #else
  101.         (void)close(crp->socket);
  102. #endif
  103.         crp->socket = RPC_ANYSOCK;
  104.         if (crp->client) {
  105.             clnt_destroy(crp->client);
  106.             crp->client = NULL;
  107.         }
  108.         if ((hp = gethostbyname(host)) == NULL)
  109.             return ((int) RPC_UNKNOWNHOST);
  110.         timeout.tv_usec = 0;
  111.         timeout.tv_sec = 5;
  112.         bcopy(hp->h_addr, (char *)&server_addr.sin_addr, hp->h_length);
  113.         server_addr.sin_family = AF_INET;
  114.         server_addr.sin_port =  0;
  115.         if ((crp->client = clntudp_create(&server_addr, (u_long)prognum,
  116.             (u_long)versnum, timeout, &crp->socket)) == NULL)
  117.             return ((int) rpc_createerr.cf_stat);
  118.         crp->valid = 1;
  119.         crp->oldprognum = prognum;
  120.         crp->oldversnum = versnum;
  121.         (void) strcpy(crp->oldhost, host);
  122.     }
  123.     tottimeout.tv_sec = 25;
  124.     tottimeout.tv_usec = 0;
  125.     clnt_stat = clnt_call(crp->client, procnum, inproc, in,
  126.         outproc, out, tottimeout);
  127.     /* 
  128.      * if call failed, empty cache
  129.      */
  130.     if (clnt_stat != RPC_SUCCESS)
  131.         crp->valid = 0;
  132.     return ((int) clnt_stat);
  133. }
  134.